#include "Linear.h"
#include "PointSegment.h"


float Linear::Getlength()
{
	float sum = 0;
	for (int i=0;i<(int)m_Segments.size()-1;i++)
		sum += m_Segments[i].GetLength();
	return sum;
}

void Linear::GetInfoByPercent(float num, Ogre::Vector3& pos, Ogre::Vector3 &ori)
{
	float len = Getlength();
	float poslen = len*num;
	poslen = poslen>len?len:poslen;
	for (int i=0;i<(int)m_Segments.size();i++)
	{
		if (poslen-m_Segments[i].GetLength() > 0)
			poslen-=m_Segments[i].GetLength();
		else
		{
			pos = m_Segments[i].GetPosByPercent(poslen/m_Segments[i].GetLength());
			ori = m_Segments[i].GetOriByPercent(poslen/m_Segments[i].GetLength());
			break;
		}
	}
}

void Linear::BuildSegments()
{
	ControlPoints &refpoints = *m_refpoints;
	for (int i=0;i<(int)refpoints.size();i++)
		refpoints[i].Update();
	m_Segments.clear();
	for (int i=0;i<(int)refpoints.size()-1;i++)
		m_Segments.push_back(PointSegment(refpoints[i], refpoints[i+1]));
	m_Segments.push_back(PointSegment(refpoints.back(), refpoints[0]));
}

